package com.anolesoft.yr.mbus.pre.crtl;

import cn.hutool.core.bean.BeanUtil;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.mbus.pre.dto.PrePromoteDeleteDTO;
import com.anolesoft.yr.mbus.pre.dto.PrePromoteQueryDTO;
import com.anolesoft.yr.mbus.pre.dto.PrePromoteSaveDTO;
import com.anolesoft.yr.mbus.pre.dto.PrePromoteUpdateDTO;
import com.anolesoft.yr.mbus.pre.entity.PrePromote;
import com.anolesoft.yr.mbus.pre.service.PrePromoteService;
import com.anolesoft.yr.mbus.pre.vo.PrePromoteQueryVO;
import com.anolesoft.yr.mbus.pre.vo.PrePromoteQueryVO2;
import com.anolesoft.yr.mbus.util.GlobalLoginInfo;
import com.anolesoft.yr.mbus.util.StrCP;
import com.anolesoft.yr.util.PageInfo;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@CrossOrigin
@RequestMapping(value = "/prePromote")
public class PrePromoteController {

    @Autowired
    PrePromoteService prePromoteService;

    //新增推广记录
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult savePrePromote(@RequestBody PrePromote prePromote) throws Exception {
        String prePromoteUid = StrCP.createObjuid32(UUID.randomUUID().toString());
        PrePromoteSaveDTO prePromoteSaveDTO = BeanUtil.copyProperties(prePromote, PrePromoteSaveDTO.class);
        prePromoteSaveDTO.setObjuid(prePromoteUid);
        prePromoteSaveDTO.setFacCode("");
        prePromoteSaveDTO.setState(0);
        prePromoteSaveDTO.setCcUserUid(GlobalLoginInfo.getGlobalLoginUser());
        prePromoteSaveDTO.setCcTime(GlobalLoginInfo.getGlobalLoginTime());
        prePromoteSaveDTO.setDelFlag(0);

        String str = "";
        try {
            str = prePromoteService.savePrePromote(prePromoteSaveDTO);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }
        return new AjaxResult(200, str);
    }

    //修改推广记录
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult updatePrePromote(@RequestBody PrePromote prePromote) throws Exception {
        PrePromoteUpdateDTO prePromoteUpdateDTO = BeanUtil.copyProperties(prePromote, PrePromoteUpdateDTO.class);
        prePromoteUpdateDTO.setMmUserUid(GlobalLoginInfo.getGlobalLoginUser());
        prePromoteUpdateDTO.setMmTime(GlobalLoginInfo.getGlobalLoginTime());

        String str = "";
        try {
            str = prePromoteService.updatePrePromote(prePromoteUpdateDTO);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }
        return new AjaxResult(200, str);
    }

    //删除推广记录
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult deletePrePromote(@RequestBody List<PrePromote> prePromoteList) throws Exception {
        List prePromoteDeleteDTOList = new ArrayList();
        PrePromoteDeleteDTO prePromoteDeleteDTO = null;

        for (int i = 0; i < prePromoteList.size(); i++) {
            PrePromote prePromote = prePromoteList.get(i);

            prePromoteDeleteDTO = new PrePromoteDeleteDTO();
            prePromoteDeleteDTO.setObjuid(prePromote.getObjuid());
            prePromoteDeleteDTOList.add(prePromoteDeleteDTO);
        }

        String str = "";
        try {
            str = prePromoteService.deletePrePromote(prePromoteDeleteDTOList);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }
        return new AjaxResult(200, str);
    }

    //提交推广记录
    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult submitPrePromote(@RequestBody List<PrePromote> prePromoteList) throws Exception {
        List prePromoteDeleteDTOList = new ArrayList();
        PrePromoteDeleteDTO prePromoteDeleteDTO = null;

        for (int i = 0; i < prePromoteList.size(); i++) {
            PrePromote prePromote = prePromoteList.get(i);

            prePromoteDeleteDTO = new PrePromoteDeleteDTO();
            prePromoteDeleteDTO.setObjuid(prePromote.getObjuid());
            prePromoteDeleteDTOList.add(prePromoteDeleteDTO);
        }

        String str = "";
        try {
            str = prePromoteService.submitPrePromote(prePromoteDeleteDTOList);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }
        return new AjaxResult(200, str);
    }

    //根据主键查询推广记录唯一数据
    @RequestMapping(value = "/queryByUid", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult AjaxResult(@RequestBody PrePromoteQueryDTO prePromoteQueryDTO) throws Exception {
        Map<String, Object> maps = prePromoteService.queryPrePromoteByUid(prePromoteQueryDTO.getObjuid());
        PrePromoteQueryVO2 prePromoteQueryVO2 = (PrePromoteQueryVO2) maps.get("prePromoteQueryVO2");

        String jsons = "";
        jsons += "{";
        jsons += "\"promote\":";
        if (prePromoteQueryVO2 != null) {
            jsons += "[";
            jsons += new Gson().toJson(prePromoteQueryVO2);
            jsons += "]";
        }
        jsons += "}";

        return new AjaxResult(200, "SUCCESS", jsons);
    }

    //根据条件查询推广记录所有数据
    @RequestMapping(value = "/queryAll", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult queryPrePromoteAll(String infoSource, String sourceTime, String intoCompany, int page, int rows) throws Exception {
        Map<String, Object> maps = prePromoteService.queryPrePromoteAll(infoSource, sourceTime, intoCompany, page, rows);
        int count = (int) maps.get("count");
        List prePromoteVOList = (List) maps.get("prePromoteVOList");

        return new AjaxResult(200, "SUCCESS", new PageInfo(count, prePromoteVOList));
    }

    //根据条件查询推广记录所有数据
    @RequestMapping(value = "/queryAll2", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult queryPrePromoteAll2(String infoSource, String sourceTime, String intoCompany, String state, int page, int rows) throws Exception {
        Map<String, Object> maps = prePromoteService.queryPrePromoteAll2(infoSource, sourceTime, intoCompany, state, page, rows);
        int count = (int) maps.get("count");
        List prePromoteVOList = (List) maps.get("prePromoteVOList");

        return new AjaxResult(200, "SUCCESS", new PageInfo(count, prePromoteVOList));
    }

    //根据条件查询推广记录所有数据
    @RequestMapping(value = "/queryAll3", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult queryPrePromoteAll3(String infoSource, String sourceTime, String intoCompany, int page, int rows) throws Exception {
        Map<String, Object> maps = prePromoteService.queryPrePromoteAll3(infoSource, sourceTime, intoCompany, page, rows);
        int count = (int) maps.get("count");
        List prePromoteVOList = (List) maps.get("prePromoteVOList");

        return new AjaxResult(200, "SUCCESS", new PageInfo(count, prePromoteVOList));
    }

    //查询人员信息（临时查询，正常应该在人员接口中）
    @RequestMapping(value = "/queryFacEmployeeName", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult queryFacEmployeeName() throws Exception {
        List prePromoteQueryVOList = prePromoteService.queryFacEmployeeName();

        String jsons = "";
        jsons += "[";
        if (prePromoteQueryVOList.size() > 0) {
            for (int i = 0; i < prePromoteQueryVOList.size(); i++) {
                PrePromoteQueryVO prePromoteQueryVO = (PrePromoteQueryVO) prePromoteQueryVOList.get(i);
                jsons += new Gson().toJson(prePromoteQueryVO);
                if (i < (prePromoteQueryVOList.size() - 1)) {
                    jsons += ",";
                }
            }
        }
        jsons += "]";

        return new AjaxResult(200, "SUCCESS", jsons);
    }

    //设置产品套餐信息
    @RequestMapping(value = "/assign", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult setTimeProdPackage(@RequestBody PrePromote prePromote) throws Exception {
        String str = "";
        try {
            str = prePromoteService.assignPrePromote(prePromote);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }

        return new AjaxResult(200, str);
    }

    //查询地区信息（临时查询，正常应该在地区接口中）
    @RequestMapping(value = "/queryRegionName", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult queryRegionName() throws Exception {
        List prePromoteQueryVOList = prePromoteService.getRegionName();

        String jsons = "";
        jsons += "[";
        if (prePromoteQueryVOList.size() > 0) {
            for (int i = 0; i < prePromoteQueryVOList.size(); i++) {
                PrePromoteQueryVO prePromoteQueryVO = (PrePromoteQueryVO) prePromoteQueryVOList.get(i);
                jsons += new Gson().toJson(prePromoteQueryVO);
                if (i < (prePromoteQueryVOList.size() - 1)) {
                    jsons += ",";
                }
            }
        }
        jsons += "]";

        return new AjaxResult(200, "SUCCESS", jsons);
    }

    //查询客户名称信息（临时查询，正常应该在客户接口中）
    //查询客户名称
    @RequestMapping(value = "/queryCrmBaseName", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult queryCrmBaseName() throws Exception {
        List prePromoteQueryVOList = prePromoteService.queryCrmBaseName();

        String jsons = "";
        jsons += "[";
        if (prePromoteQueryVOList.size() > 0) {
            for (int i = 0; i < prePromoteQueryVOList.size(); i++) {
                PrePromoteQueryVO prePromoteQueryVO = (PrePromoteQueryVO) prePromoteQueryVOList.get(i);
                jsons += new Gson().toJson(prePromoteQueryVO);
                if (i < (prePromoteQueryVOList.size() - 1)) {
                    jsons += ",";
                }
            }
        }
        jsons += "]";
        return new AjaxResult(200, "SUCCESS", jsons);
    }

    //查询客户名称信息（临时查询，正常应该在客户接口中）
    //查询客户名称
    @RequestMapping(value = "/queryCrmBase2", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult queryCrmBase2(@RequestBody PrePromoteQueryDTO prePromoteQueryDTO) throws Exception {
        PrePromoteQueryVO prePromoteQueryVO = prePromoteService.queryCrmBase2(prePromoteQueryDTO.getObjuid());

        String jsons = "";
        if (prePromoteQueryVO != null) {
            jsons += "[";
            jsons += new Gson().toJson(prePromoteQueryVO);
            jsons += "]";
        }

        return new AjaxResult(200, "SUCCESS", jsons);
    }

    //根据主键查询推广记录，用于询盘回访界面的显示
    @RequestMapping(value = "/queryPrePromoteAll4", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult queryPrePromoteAll4(String objuid) throws Exception {
        PrePromoteQueryVO prePromoteQueryVO = prePromoteService.queryPrePromoteByUid2(objuid);

        String jsons = "";
        if (prePromoteQueryVO != null) {
            jsons += "[";
            jsons += new Gson().toJson(prePromoteQueryVO);
            jsons += "]";
        }

        return new AjaxResult(200, "SUCCESS", jsons);
    }

}
